package com.CST.controller.zx;


import com.CST.common.api.ResultObject;
import com.CST.entity.zx.ZxDataentry;
import com.CST.service.zx.IZxDataentryService;
import com.CST.util.ExcelTransfer;
import com.CST.vo.zx.ZxDataentryVO;
import io.swagger.annotations.*;

import java.util.*;

import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ly
 * @since 2023-02-27
 */
@Api(tags = {"数据项相关操作"})
@RestController
@RequestMapping("/zxDataentry")

public class ZxDataentryController {

    @Autowired
    private IZxDataentryService targetService;

    @PostMapping("/page")
    @ApiOperation("分页查询,必要字段 数据字典编码 dictCode 第几页 pageNo 每页几条数据 每页几条数据" +
            "选择字段 entryName 数据字典名称")
    public ResultObject<List<ZxDataentry>> page(@RequestBody ZxDataentry zxDataentry) {
        return targetService.page(zxDataentry);
    }

    @GetMapping("/getEntryByDictId/{dictCode}")
    @ApiOperation("查询某个数据字典下的数据项,必要字段数据字典编号 dictCode ")
    public ResultObject<List<ZxDataentryVO>> getEntryByDictId(@PathVariable String dictCode) {
        return targetService.getEntryByDictId(dictCode);
    }

    @PostMapping("/save")
    @ApiOperation("新建数据项，必要参数除页面数据外还要，dictCode 数据字典编码")
    public ResultObject<String> save(@RequestBody ZxDataentry zxDataentry) {
        return targetService.saveEntry(zxDataentry);
    }

    @PutMapping("/update")
    @ApiOperation("修改数据项，必传参数id，数据项的id")
    public ResultObject<String> update(@RequestBody ZxDataentry zxDataentry) {
        return targetService.updateEntry(zxDataentry);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("逻辑删除数据项,0可用，1逻辑删除")
    public ResultObject<String> delete(@PathVariable Long id) {
        return targetService.removeById(id)?ResultObject.success("删除成功"):ResultObject.failed("删除失败");
    }

    @PostMapping("/unEnable")
    @ApiOperation("停用数据项，需要id，和enableFlag（0停用，1启用）")
    public ResultObject<String> editEnable(@RequestBody ZxDataentry zxDataentry) {
        return targetService.editById(zxDataentry)?ResultObject.success("修改状态成功"):ResultObject.failed("修改状态失败");
    }

    @PostMapping("/saveFile")
    @ApiOperation("导入文件")
    public ResultObject saveFile(MultipartFile file) throws Exception {
        try {
            targetService.saveFile(file);
        } catch (Exception e) {
            throw new Exception("导入失败");
        }
        return ResultObject.successReturn("导入成功");
    }

    @PostMapping("/download/{isMoBan}/{dictCode}")
    @ApiOperation("下载文件，必要参数HttpServletResponse response,isMoBan(0表示下载模版，1表示下载文件),dictCode 数据字典的编码" +
            "选择参数entryName 数据项名称,dictId 数据字典的id")
    public void download(HttpServletResponse response,@PathVariable Integer isMoBan,String entryName,@PathVariable String dictCode) {
        if (isMoBan == 0){
            ExcelTransfer<ZxDataentry> excelTransfer = new ExcelTransfer<>();
            try {
                excelTransfer.exportExcel(response,null,"数据字典模板","数据项", ZxDataentry.class);
            } catch (Exception e) {

                throw new RuntimeException(e);
            }
            return;
        }
        targetService.download(response,entryName,dictCode);
    }

}

